package cn.wolfcode.shop.cloud.web.controller;

import cn.wolfcode.shop.cloud.common.Result;
import cn.wolfcode.shop.cloud.redis.RedisService;
import cn.wolfcode.shop.cloud.redis.SecKillKeyPrefix;
import cn.wolfcode.shop.cloud.service.ISeckillGoodService;
import cn.wolfcode.shop.cloud.vo.SeckillGoodVO;
import com.netflix.discovery.converters.Auto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/seckillGood")
public class SeckillGoodsController {
    @Autowired
    private ISeckillGoodService seckillGoodService;

    @Autowired
    private RedisService redisService;


    @RequestMapping("/query")
    public Result<List<SeckillGoodVO>> query(){
        List<SeckillGoodVO> result=seckillGoodService.queryCache();
        System.out.println(result);
        return Result.success(result);
    }
    @RequestMapping("/seckillGoodDetail")
    public Result<SeckillGoodVO> seckillGoodDetail(Long seckillId){

        return Result.success(seckillGoodService.queryById(seckillId));
    }
    @RequestMapping("/initData")
    public String initData(){
        List<SeckillGoodVO> seckillGoodVos = seckillGoodService.query();
        for (SeckillGoodVO seckillGoodVo : seckillGoodVos) {
            redisService.set(SecKillKeyPrefix.SECKILL_STOCK_COUNT,seckillGoodVo.getGoodId()+"",seckillGoodVo.getStockCount());
            redisService.hset(SecKillKeyPrefix.SECKILL_GOOD_HASH,"",seckillGoodVo.getId()+"",seckillGoodVo);
//查询出一个数据key
        }

        return "初始化成功";
    }
}
